/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package doghost.utils;

import java.awt.Component;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author duo
 */
public class CustomCurrencyRenderer extends DefaultTableCellRenderer {
    
        protected DecimalFormat formatter;

    public CustomCurrencyRenderer() {
        super();
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, 
            boolean isSelected, boolean hasFocus, int row, int column) {
        if (formatter == null) {
            DecimalFormatSymbols decimalFormatSymbols = 
                    new DecimalFormatSymbols();
            decimalFormatSymbols.setDecimalSeparator(',');
            decimalFormatSymbols.setGroupingSeparator('.');
            decimalFormatSymbols.setCurrencySymbol("R$");
            formatter = new DecimalFormat("###,##0.00", decimalFormatSymbols);
        }
        if (value instanceof Double) {
            value = formatter.format((Double) value);
        }
        if (value instanceof BigDecimal) {
            BigDecimal bdValue = (BigDecimal) value;
            Double dbValue = bdValue.doubleValue();
            value = formatter.format((Double) dbValue);
        }
        JLabel l =
                (JLabel) super.getTableCellRendererComponent(table, value, isSelected,
                hasFocus, row, column);
        l.setHorizontalAlignment(RIGHT);
        return l;
    }
}
